package com.junxonline.instant.api.service.api.impl;

import cn.hutool.core.date.DateUtil;
import com.junxonline.instant.api.service.api.TerminalService;
import com.junxonline.instant.common.constant.RedisConstant;
import com.junxonline.instant.common.constant.TerminalInfoConstant;
import com.junxonline.instant.common.util.api.TokenUtils;
import com.junxonline.instant.common.util.common.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Netty服务接口实现类
 *
 * @author JunX
 * @date 2021-09-03
 */
@Service
@Transactional
public class TerminalServiceImpl implements TerminalService {

    @Value("${auth.tokenExpireTime}")
    private Long tokenExpireTime;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String login(String terminalCode) {
        Long currentTime = DateUtil.current();
        String token = tokenUtils.sign(terminalCode, currentTime);
        // token存入redis设定过期时间
        redisUtils.hset(RedisConstant.KEY_TERMINAL_AUTH + terminalCode, TerminalInfoConstant.KEY_TOKEN, token, tokenExpireTime / 1000);
        return token;
    }

}
